package Test.MQ.ACK;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    // 自动ACK队列
    public static final String AUTO_ACK_QUEUE = "auto.ack.queue";
    // 手动ACK队列
    public static final String MANUAL_ACK_QUEUE = "manual.ack.queue";

    @Bean
    public Queue autoAckQueue() {
        return new Queue(AUTO_ACK_QUEUE);
    }

    @Bean
    public Queue manualAckQueue() {
        return new Queue(MANUAL_ACK_QUEUE);
    }

    // 自动ACK的监听容器工厂
    @Bean(name = "autoAckContainerFactory")
    public SimpleRabbitListenerContainerFactory autoAckContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO); // 自动ACK
        return factory;
    }

    // 手动ACK的监听容器工厂
    @Bean(name = "manualAckContainerFactory")
    public SimpleRabbitListenerContainerFactory manualAckContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL); // 手动ACK
        return factory;
    }
}